﻿using IsmsExplorer.Infrastructure.Helper;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace IsmsExplorer.UnitTests
{
    /// <summary>
    ///This is a test class for Iso27001HelperTest and is intended
    ///to contain all Iso27001HelperTest Unit Tests
    ///</summary>
    [TestClass()]
    public class Iso27001HelperTest {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext {
            get {
                return testContextInstance;
            }
            set {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        /// <summary>
        ///A test for CompareSections
        ///</summary>
        [TestMethod()]
        public void CompareSectionsTest() {
            
        }

        /// <summary>
        ///A test for CompareSectionsRegex
        ///</summary>
        [TestMethod()]
        public void CompareSectionsRegexTest() {
            Assert.AreEqual(0, Iso27001Helper.CompareSectionsRegex("A.2.3.1", "A.2.3.1"));
            Assert.AreEqual(-1, Iso27001Helper.CompareSectionsRegex("A.2.3.1", "A.2.3.2"));
            Assert.AreEqual(1, Iso27001Helper.CompareSectionsRegex("A.2.3.1", "A.1.3.1"));
            Assert.AreEqual(1, Iso27001Helper.CompareSectionsRegex("A.45.3.41", "A.41.3.1"));
            Assert.AreEqual(0, Iso27001Helper.CompareSectionsRegex("A.45.3.41   ", "  A.45.3.41"));
            Assert.AreEqual(-1, Iso27001Helper.CompareSectionsRegex("A.45.1.41   ", "  A.45.3.41"));

            Assert.AreEqual(0, Iso27001Helper.CompareSections("A.2.3.1", "A.2.3.1"));
            Assert.AreEqual(-1, Iso27001Helper.CompareSections("A.2.3.1", "A.2.3.2"));
            Assert.AreEqual(1, Iso27001Helper.CompareSections("A.2.3.1", "A.1.3.1"));
            Assert.AreEqual(1, Iso27001Helper.CompareSections("A.45.3.41", "A.41.3.1"));
            // NOTE: the CompareSections() method cannot deal with spaces in the beginning, spaces in the end should be ok!
            Assert.AreEqual(0, Iso27001Helper.CompareSections("A.45.3.41   ", "A.45.3.41 "));
            Assert.AreEqual(-1, Iso27001Helper.CompareSections("A.45.1.41   ", "A.45.3.41"));
        }

        /// <summary>
        ///A test for ExtractInt
        ///</summary>
        [TestMethod()]
        [DeploymentItem("IsmsExplorer.Infrastructure.dll")]
        public void ExtractIntTest() {
            //string s = string.Empty; // TODO: Initialize to an appropriate value
            //int startAt = 0; // TODO: Initialize to an appropriate value
            //int startAtExpected = 0; // TODO: Initialize to an appropriate value
            //int expected = 0; // TODO: Initialize to an appropriate value
            //int actual;
            //actual = Iso27001Helper_Accessor.ExtractInt(s, ref startAt);
            //Assert.AreEqual(startAtExpected, startAt);
            //Assert.AreEqual(expected, actual);
            //Assert.Inconclusive("Verify the correctness of this test method.");
        }

        /// <summary>
        ///A test for GetSections
        ///</summary>
        [TestMethod()]
        public void GetSectionsTest() {
            //string refID = string.Empty; // TODO: Initialize to an appropriate value
            //string x = string.Empty; // TODO: Initialize to an appropriate value
            //string xExpected = string.Empty; // TODO: Initialize to an appropriate value
            //string y = string.Empty; // TODO: Initialize to an appropriate value
            //string yExpected = string.Empty; // TODO: Initialize to an appropriate value
            //string z = string.Empty; // TODO: Initialize to an appropriate value
            //string zExpected = string.Empty; // TODO: Initialize to an appropriate value
            //bool expected = false; // TODO: Initialize to an appropriate value
            //bool actual;
            //actual = Iso27001Helper.GetSections(refID, out x, out y, out z);
            //Assert.AreEqual(xExpected, x);
            //Assert.AreEqual(yExpected, y);
            //Assert.AreEqual(zExpected, z);
            //Assert.AreEqual(expected, actual);
            //Assert.Inconclusive("Verify the correctness of this test method.");

            string x, y, z;
            Assert.IsTrue(Iso27001Helper.GetSections("A.1.2.3", out x, out y, out z));
            Assert.IsTrue(x.Equals("1") && y.Equals("2") && z.Equals("3"));
            Assert.IsTrue(Iso27001Helper.GetSections("  A.99.2.33", out x, out y, out z));
            Assert.IsTrue(x.Equals("99") && y.Equals("2") && z.Equals("33"));
            Assert.IsTrue(Iso27001Helper.GetSections(" A.1.22.82   ", out x, out y, out z));
            Assert.IsTrue(x.Equals("1") && y.Equals("22") && z.Equals("82"));
            Assert.IsFalse(Iso27001Helper.GetSections("A1.2.9", out x, out y, out z));
            Assert.IsTrue((x == null) && (y == null) && (z == null));
            Assert.IsTrue(Iso27001Helper.GetSections("A 5.6.55", out x, out y, out z));
            Assert.IsTrue((x == "5") && (y == "6") && (z == "55"));
        }

        /// <summary>
        ///A test for Validate
        ///</summary>
        [TestMethod()]
        public void ValidateTest() {
            Assert.IsTrue(Iso27001Helper.Validate("A.1.2.3"));
            Assert.IsTrue(Iso27001Helper.Validate("  A.99.2.33"));
            Assert.IsTrue(Iso27001Helper.Validate(" A.1.22.82   "));
            Assert.IsTrue(Iso27001Helper.Validate(" A.10.22.82   "));
            Assert.IsTrue(Iso27001Helper.Validate("A.10.22.82   "));
            Assert.IsFalse(Iso27001Helper.Validate("A1.2.9"));
            // Updated regex to accept "A x.y.z" and not require '.' after A.
            Assert.IsTrue(Iso27001Helper.Validate("A 5.6.55"));
            Assert.IsFalse(Iso27001Helper.Validate("A.6.55"));
            Assert.IsFalse(Iso27001Helper.Validate("A.5.1.3.2"));
            Assert.IsFalse(Iso27001Helper.Validate("A.7.b.5"));
            Assert.IsFalse(Iso27001Helper.Validate("A.7 5.3"));
            Assert.IsFalse(Iso27001Helper.Validate("A.786.3.1"));
            Assert.IsFalse(Iso27001Helper.Validate("A .7.3.10"));
            // Allow prefix with 0 of single-digit sections.
            Assert.IsTrue(Iso27001Helper.Validate("A.02.3.1"));
        }
    }
}
